package com.isorf.controller;

import com.github.pagehelper.PageInfo;
import com.isorf.pojo.student;
import com.isorf.pojo.users;
import com.isorf.service.userservice;
import com.isorf.util.responseresult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class usercontroller {
    @Autowired
    private userservice userservice;
    @PostMapping("/userList")
    public responseresult<List<users>> userList() {
        List<users> userList = userservice.userList();
        if (userList == null||userList.size()==0) {
            return responseresult.failure(responseresult.ERROR, "没有用户信息");
        }else {
            return responseresult.success(userList);

        }
    }
    @PostMapping("/updateDisable")
    public responseresult<Boolean> updateDisable(@RequestBody Map<String, Object> request) {
        try {
            Integer id = (Integer) request.get("id");
            if (id == null) {
                return responseresult.failure(responseresult.FAILED, "用户ID不能为空");
            }
            int result = userservice.updateDisable(id);
            if (result > 0) {
                return responseresult.success(responseresult.SUCCESS, "修改成功", true);
            } else {
                return responseresult.failure(responseresult.FAILED, "修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return responseresult.failure(responseresult.FAILED, "修改失败: " + e.getMessage());
        }
    }
    @PostMapping("/pageList")
    public responseresult<PageInfo> pageList(@RequestBody Map<String, Object> request) {
        try {
            Integer pageNum = (Integer) request.get("pageNum");
            Integer pageSize = (Integer) request.get("pageSize");

            if (pageNum == null) pageNum = 1;
            if (pageSize == null) pageSize = 5;

            PageInfo<users> pageInfo = userservice.pageList(pageNum, pageSize);

            if (pageInfo == null || pageInfo.getList().size() == 0) {
                return responseresult.failure(responseresult.FAILED, "查询失败");
            } else {
                return responseresult.success(responseresult.SUCCESS, "查询成功", pageInfo);
            }
        } catch (Exception e) {
            return responseresult.failure(responseresult.FAILED, "查询失败: " + e.getMessage());
        }
    }










}

